#!/bin/bash

# 输出欢迎信息
echo "                                                                                                    
                                                 .::-----::.                                        
                                            .-=+*####********+=:.                                   
                                         .=*######***************=:                                 
                                       -+#######*=-::.......::-=++++=.                              
                                     -*######+-.                  .:=+=:                            
                                   :*######=.                         .::.                          
                      .-=+**######*######+.                                                         
                  .=*#%%%%%%##########**-                          ...:::::...                      
                -*%%%%%%%###########***:                      .:-=============---:.                 
              :#%%%%%%%#*+-::..    ..:.                    .-====-:..      .....:::..               
             =%%%%%%#+:                                  :=====:       :-==========-:.              
            +%%%%##*:                                  :=====:      .-=====------=++++=:            
           =%%%%##=                                  :=====:       :===-.          :=++*+.          
          .%#####=                                 :=====-        :===.              -****.         
          -######                                :=====-.         -==-          .     :***+         
          +#####+                              .======:           :===         .+      +***         
          =#####*                            .======:   .:--::.    -++-.      -*-      +*##.        
          -####**-                         :======-   -==-:::-==-   :=+++==++**-      .###*         
           *#*****.                     .:======-.   ==.       :+=.   .:====-:        *###-         
           :*******-                 .:-======-.     =-     :   -+-                 :*###+          
            :*******+=:.         .:-========-.       :=-:::-:   -+-               :+####=           
              =****++++++=======++========:.           .:::    :++.           .:=*####+.            
               .-++++++++++++++========-.                    .=+=.      .::=+*#####+-.              
                  .:==++++++======--:.                ..::-=+**=--=++***######*+-:                  
                       ..:::::..         .....:::---====+++++=+++++++===--:..                       
                                                                                                    
                        欢迎使用浪浪云工具箱                                                             
                                                                                                    "

# 检测系统信息
detect_system_info() {
    echo "检测系统信息..."
    # 打印操作系统信息
    uname -a
    # 打印CPU信息
    lscpu
    # 打印内存信息
    free -h
    # 打印磁盘使用情况
    df -h
}
# 硬盘使用情况
disk_usage() {
    df -h
}
#内存使用情况
memory_usage() {
    free -h
}
# 测速
speed_test() {
    # 检查speedtest-cli是否已经安装
    if ! command -v speedtest-cli &> /dev/null; then
        echo "speedtest-cli not found, trying to install..."
        # 检测系统类型
        if [ -f /etc/os-release ]; then
            . /etc/os-release
            OS=$NAME
        else
            OS=$(uname -s)
        fi

        case $OS in
            "CentOS Linux")
                echo "Detected CentOS. Proceed with the installation..."
                sudo yum install -y speedtest-cli
                ;;
            "Ubuntu"|"Debian GNU/Linux")
                echo "Detected Ubuntu/Debian. Proceed with the installation..."
                sudo apt-get install -y speedtest-cli
                ;;
            *)
                echo "Unsupported OS, please install speedtest-cli manually."
                return 1
                ;;
        esac
    fi

    echo "speedtest-cli is installed."
    # 运行测速
    speedtest-cli
}

# 系统工具箱
system_toolbox() {
    echo "请选择要执行的操作："
    echo "1) 查看机器信息"
    echo "2) 测速"
    echo "3) 硬盘使用情况检查"
    echo "4) 内存使用情况检查"
    echo "0) 返回主菜单"
    read -rp "选择操作[0-4]：" action
    case $action in
        1)
            detect_system_info
            ;;
        2)
            speed_test
            ;;
        3)
            disk_usage
            ;;
        4)
            memory_usage
            ;;
        0)
            echo "返回主菜单"
            return 0
            ;;
        *)
            echo "无效的操作"
            ;;
    esac
}

# 安装Docker
install_docker() {
    echo "安装Docker..."
    case "$(uname -s)" in
        Linux*)
            # 选择合适的安装命令
            if command -v apt-get >/dev/null 2>&1; then
                # 使用APT安装（适用于基于Debian的系统）
                sudo apt-get update
                sudo apt-get install -y docker.io
            elif command -v yum >/dev/null 2>&1; then
                # 使用YUM安装（适用于基于RHEL的系统）
                sudo yum update
                sudo yum install -y docker
            else
：                echo "不支持的Linux发行版或者包管理器"
                return 1
            fi
            # 启动Docker服务
            sudo systemctl start docker
            # 设置Docker开机自启
            sudo systemctl enable docker
            ;;
        *)
            echo "不支持的操作系统"
            return 1
            ;;
    esac
}

# 安装宝塔面板
install_baota() {
    echo "安装宝塔面板..."
    # 宝塔Linux面板一键安装命令
    if [ -f /usr/bin/curl ];then curl -sSO https://download.bt.cn/install/install_panel.sh;else wget -O install_panel.sh https://download.bt.cn/install/install_panel.sh;fi;bash install_panel.sh ed8484bec
}

# 安装青龙面板
install_qlpanel() {
    echo "安装青龙面板..."
    # 青龙面板一键安装命令，需要根据青龙面板的官方文档更新命令
    # 示例命令可能已经过时，请替换为最新的安装命
    curl -sSL get.docker.com | sh
    docker run -dit \
        -v $PWD/ql/data:/ql/data \
         # 冒号后面的 5700 为默认端口，如果设置了 QlPort, 需要跟 QlPort 保持一致
         -p 5700:5700 \
        # 部署路径非必须，比如 /test
        -e QlBaseUrl="/" \
        # 部署端口非必须，当使用 host 模式时，可以设置服务启动后的端口，默认 5700
        -e QlPort="5700" \
        --name qinglong \
        --hostname qinglong \
        --restart unless-stopped \
    whyour/qinglong:latest
}
# 安装1Panel
install_1panel() {
    # 检测系统类型
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        OS=$NAME
    else
        OS=$(uname -s)
    fi

    case $OS in
        "CentOS Linux")
            echo "Detected CentOS. Proceed with the installation..."
            curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sh quick_start.sh
            ;;
        "Ubuntu")
            echo "Detected Ubuntu. Proceed with the installation..."
            curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh
            ;;
        "Debian GNU/Linux")
            echo "Detected Debian. Proceed with the installation..."
            curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && bash quick_start.sh
            ;;
        *)
            echo "Unsupported OS, please install 1Panel manually."
            return 1
            ;;
    esac
}

# 打印优惠券
print_coupon() {
    echo "获得浪浪云专属福利"
    # 这里可以根据实际情况输出优惠券信息，示例：
    echo "浪浪云官网：https://idc.langlangy.com/"
    echo "新用户专享85折优惠"
}
# 换源
change_source() {
    echo "请选择要更换的源："
    echo "1) 阿里源"
    echo "2) 清华源"
    echo "3) 中科大源"
    echo "0) 返回主目录"
    read -rp "选择源[1-3]：" source
    case "$(uname -s)" in
        Linux*)
            if command -v apt-get >/dev/null 2>&1; then
                # Ubuntu系统
                case $source in
                    1)
                        # 更换为阿里源
                        sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
                        echo "deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse" | sudo tee /etc/apt/sources.list
                        sudo apt-get update
                        ;;
                    2)
                        # 更换为清华源
                        sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
                        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse" | sudo tee /etc/apt/sources.list
                        sudo apt-get update
                        ;;
                    3)
                        # 更换为中科大源
                        sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
                        echo "deb https://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse" | sudo tee /etc/apt/sources.list
                        sudo apt-get update
                        ;;
                    *)
                        echo "无效的源"
                        ;;
                esac
            elif command -v yum >/dev/null 2>&1; then
                # CentOS系统
                case $source in
                    1)
                        # 更换为阿里源
                        sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
                        sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
                        sudo yum makecache
                        ;;
                    2)
                        # 更换为清华源
                        sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
                        sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/help/centos/
                        sudo yum makecache
                        ;;
                    3)
                        # 更换为中科大源
                        sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
                        sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.ustc.edu.cn/centos/
                        sudo yum makecache
                        ;;
                    *)
                        echo "无效的源"
                        ;;
                esac
            else
                echo "不支持的Linux发行版或者包管理器"
                return 1
            fi
            ;;
        *)
            echo "不支持的操作系统"
            return 1
            ;;
    esac
}
# bbr网络优化
enable_bbr() {
    # Check if the kernel version is 4.9 or higher
    KERNEL=$(uname -r | cut -d- -f1)
    if [[ $(echo "$KERNEL 4.9" | awk '{print (\$1 > \$2)}') -ne 1 ]]; then
        echo "Kernel version is less than 4.9. Please upgrade your kernel."
        return 1
    fi

    # Check if BBR is available
    if [[ $(sysctl net.ipv4.tcp_available_congestion_control | grep -c 'bbr') -eq 0 ]]; then
        echo "BBR is not available on this system."
        return 1
    fi

    # Enable BBR
    sysctl -w net.core.default_qdisc=fq
    sysctl -w net.ipv4.tcp_congestion_control=bbr

    # Make the changes permanent
    echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
    echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

    # Check if the changes have been applied
    if [[ $(sysctl net.ipv4.tcp_congestion_control | grep -c 'bbr') -eq 1 ]]; then
        echo "BBR has been enabled."
    else
        echo "Failed to enable BBR."
    fi
}

# 主菜单
main_menu() {
    echo "请选择要执行的操作："
    echo "1) 系统工具箱"
    echo "2) 安装Docker"
    echo "3) 安装宝塔面板"
    echo "4) 安装青龙面板"
    echo "5) 安装1Panel"
    echo "6) 获得浪浪云专属优惠"
    echo "7) 更换源"
    echo "8) BBR网络优化"
    echo "0) 退出"
    read -rp "选择操作[0-8]：" action
    case $action in
        1)
            system_toolbox
            ;;
        2)
            install_docker
            ;;
        3)
            install_baota
            ;;
        4)
            install_qlpanel
            ;;
        5)
            install_1panel
            ;;
        6)
            print_coupon
            ;;
        7)
            change_source
            ;;
        8)
            enable_bbr
            ;;
        0)
            echo "退出工具箱"
            exit 0
            ;;
        *)
            echo "无效的操作"
            ;;
    esac
}

# 循环显示主菜单
while true; do
    main_menu
done

